package com.xuexiang.yoyo.sql.mapper;

import android.content.ContentValues;
import android.database.Cursor;

import com.fcat.base.common.util.ArrayUtil;
import com.xuexiang.yoyo.constant.HesAppStatic;
import com.xuexiang.yoyo.entity.User;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ssen on 2023/4/10
 */
public class UserMapper {

    public static final String TABLE = "user";

    public static final String CREATE_TABLE_SQL = "CREATE TABLE " + TABLE + "(id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "username TEXT," +
            "password TEXT," +
            "token TEXT," +
            "cell_number TEXT," +
            "email TEXT," +
            "name TEXT," +
            "read_meter_model INTEGER," +
            "read_meter_protocol INTEGER);";

    public static void insert(User user) {
        ContentValues userContentValues = new ContentValues();
        userContentValues.put("username", user.getUsername());
        userContentValues.put("password", user.getPassword());
        userContentValues.put("token", user.getToken());
        userContentValues.put("cell_number", user.getCellNumber());
        userContentValues.put("email", user.getEmail());
        userContentValues.put("name", user.getName());
        userContentValues.put("read_meter_model", user.getReadMeterModel());
        userContentValues.put("read_meter_protocol", user.getReadMeterProtocol());
        HesAppStatic.getmDataBaseOpenHelper().insert(TABLE, userContentValues);
    }


    public static User queryByUsername(String username) {
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where username='" + username + "'");
        List<User> resultList = getUsers(cursor);
        User res = null;
        if (!ArrayUtil.isEmpty(resultList)) res = resultList.get(0);
        cursor.close();
        return res;
    }

    public static User login(String username, String password) {
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where username='" + username + "'and password='" + password + "'");
        List<User> resultList = getUsers(cursor);
        User res = null;
        if (!ArrayUtil.isEmpty(resultList)) res = resultList.get(0);
        cursor.close();
        return res;
    }


    public static void updateTokenByUsername(User user) {
        ContentValues values = new ContentValues();
        values.put("token", user.getToken());
        values.put("password", user.getPassword());
        String whereClause = "username=?";
        HesAppStatic.getmDataBaseOpenHelper().update(TABLE, values, whereClause, new String[]{user.getUsername().trim()});
    }


    public static void updateReadMeterModelByUsername(Integer readMeterModel) {
        ContentValues values = new ContentValues();
        values.put("read_meter_model", readMeterModel);
        String whereClause = "username=?";
        HesAppStatic.getmDataBaseOpenHelper().update(TABLE, values, whereClause, new String[]{HesAppStatic.getUsername().trim()});
    }

    public static void updateReadMeterProtocolByUsername(Integer readMeterProtocol) {
        ContentValues values = new ContentValues();
        values.put("read_meter_protocol", readMeterProtocol);
        String whereClause = "username=?";
        HesAppStatic.getmDataBaseOpenHelper().update(TABLE, values, whereClause, new String[]{HesAppStatic.getUsername().trim()});
    }


    private static List<User> getUsers(Cursor cursor) {
        List<User> resultList = new ArrayList<>();
        while (cursor.moveToNext()) {
            User item = new User();
            item.setId(cursor.getLong(0));
            item.setUsername(cursor.getString(1));
            item.setPassword(cursor.getString(2));
            item.setToken(cursor.getString(3));
            item.setCellNumber(cursor.getString(4));
            item.setEmail(cursor.getString(5));
            item.setName(cursor.getString(6));
            item.setReadMeterModel(cursor.getInt(7));
            item.setReadMeterProtocol(cursor.getInt(8));
            resultList.add(item);
        }
        return resultList;
    }

    public static void deleteUser() {
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() +"'",null);
    }
}
